<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryRef"
      :inline="true"
      label-width="120px"
    >
      <el-form-item label="巡检计划ID" prop="pollingId">
        <el-input
          v-model="queryParams.pollingId"
          placeholder="请输入"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="巡检组长ID" prop="pollingGroupId">
        <el-input
          v-model="queryParams.pollingGroupId"
          placeholder="请输入"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="巡检人员ID" prop="pollingGather">
        <el-input
          v-model="queryParams.pollingGather"
          placeholder="请输入"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="巡检分区ID" prop="pollingDivGather">
        <el-input
          v-model="queryParams.pollingDivGather"
          placeholder="请输入"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="巡检时间" style="width: 360px">
        <el-date-picker
          v-model="dateRange"
          format="YYYY-MM-DD"
          value-format="YYYY-MM-DD"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        />
      </el-form-item>
      <el-form-item style="margin-left: 120px">
        <el-button
          type="primary"
          icon="Search"
          @click="handleQuery"
          v-hasPermi="['operation:inspect:plans:query']"
          >搜索</el-button
        >
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['operation:inspect:plans:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['operation:inspect:plans:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['operation:inspect:plans:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['operation:inspect:plans:export']"
          >导出</el-button
        >
      </el-col>
    </el-row>

    <el-table
      v-loading="loading"
      :data="plansList"
      :row-class-name="tableRowClassName"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="50" align="center" />
      <el-table-column label="序号" width="80" align="center" type="index" />
      <el-table-column label="巡检计划ID" align="center" prop="pollingId">
        <template #default="{ row }"> No.{{ row.pollingId }} </template>
      </el-table-column>
      <el-table-column
        label="巡检组长ID"
        align="center"
        prop="pollingGroupId"
      />
      <el-table-column
        label="巡检人员ID集合"
        align="center"
        prop="pollingGather"
      />
      <el-table-column
        label="开始时间"
        width="150"
        align="center"
        prop="startTime"
      />
      <el-table-column
        label="结束时间"
        width="150"
        align="center"
        prop="finishTime"
      />
      <el-table-column
        label="巡检分区ID集合"
        align="center"
        prop="pollingDivGather"
      />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding  fixed-width"
      >
        <template #default="{ row }">
          <el-button
            type="text"
            icon="View"
            @click="handleView(row)"
            v-hasPermi="['operation:inspect:plans:detail']"
            >详细</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改巡检计划管理对话框 -->
    <el-dialog :title="title" v-model="open" width="600px" append-to-body>
      <el-form
        :model="form"
        :rules="rules"
        ref="inspectPlansRef"
        label-width="120px"
      >
        <el-row>
          <el-col :span="24">
            <el-form-item label="巡检计划ID" prop="pollingId">
              <el-input
                v-model="form.pollingId"
                placeholder="请输入"
                clearable
              />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="巡检组长ID" prop="pollingGroupId">
              <el-input
                v-model="form.pollingGroupId"
                placeholder="请输入"
                clearable
              />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="巡检人员ID集合" prop="pollingGather">
              <el-input
                v-model="form.pollingGather"
                placeholder="请输入"
                clearable
              />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="巡检分区ID集合" prop="pollingDivGather">
              <el-input
                v-model="form.pollingDivGather"
                placeholder="请输入"
                clearable
              />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="开始与结束时间">
              <el-date-picker
                v-model="dateRange2"
                type="datetimerange"
                format="YYYY-MM-DD HH:mm"
                value-format="YYYY-MM-DD HH:mm"
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                style="width: 450px"
              />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input
                type="textarea"
                :rows="2"
                placeholder="请输入内容"
                v-model="form.remark"
                clearable
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>

    <!-- 巡检计划详细 -->
    <el-dialog
      title="巡检计划详细"
      v-model="openView"
      width="550px"
      append-to-body
    >
      <el-form :model="form" label-width="130px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="巡检计划ID：">{{
              'No.' + form.pollingId
            }}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="巡检组长ID：">{{
              form.pollingGroupId
            }}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="巡检人员ID集合：">{{
              form.pollingGather
            }}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="巡检分区ID集合：">{{
              form.pollingDivGather
            }}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="开始时间：">
              {{ form.startTime }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="结束时间：">
              {{ form.finishTime }}
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="备注：">{{ form.remark }}</el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="openView = false">关 闭</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>
<script setup name="inspectPlans">
import {
  listPlans,
  getPlans,
  delPlans,
  addPlans,
  updatePlans
} from '@/api/operation/inspect/plans'

const { proxy } = getCurrentInstance()

const plansList = ref([])
const open = ref(false)
const loading = ref(true)
const sns = ref([])
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const openView = ref(false)
const title = ref('')
const total = ref(0)
const dateRange = ref([])
const dateRange2 = ref([])

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10
  },
  rules: {
    pollingId: [
      { required: true, message: '巡检计划ID不能为空', trigger: 'blur' }
    ],
    pollingGroupId: [
      { required: true, message: '巡检组长ID不能为空', trigger: 'blur' }
    ]
  }
})

const { queryParams, form, rules } = toRefs(data)

/** 查询巡查计划列表 */
function getList() {
  loading.value = true
  const [startTime, finishTime] = dateRange.value ?? []
  listPlans({ ...queryParams.value, startTime, finishTime }).then(
    (response) => {
      plansList.value = response.rows
      total.value = response.total
      loading.value = false
    }
  )
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  dateRange.value = []
  proxy.resetForm('queryRef')
  handleQuery()
}

/* 重置新增和修改表单 */
function reset() {
  dateRange2.value = []
  form.value = {}
  proxy.resetForm('inspectPlansRef')
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = '添加巡检计划'
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const plansId = row.id || ids.value
  getPlans(plansId).then((response) => {
    form.value = response.data
    dateRange2.value = [response.data.startTime, response.data.finishTime]
    open.value = true
    title.value = '修改巡检计划'
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const plansIds = row.id || ids.value
  proxy.$modal
    .confirm('是否确认删除序号为："' + sns.value + '"的数据项?')
    .then(function () {
      return delPlans(plansIds)
    })
    .then(() => {
      getList()
      proxy.$modal.msgSuccess('删除成功')
    })
    .catch(() => {})
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download(
    'operations/polling/export',
    {
      ...queryParams.value
    },
    `polling_${new Date().getTime()}.xlsx`
  )
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs['inspectPlansRef'].validate((valid) => {
    if (valid) {
      let params = Object.assign({}, form.value, {
        startTime: dateRange2.value[0],
        finishTime: dateRange2.value[1]
      })
      if (form.value.id != undefined) {
        updatePlans(params).then((response) => {
          proxy.$modal.msgSuccess('修改成功')
          open.value = false
          getList()
        })
      } else {
        addPlans(params).then((response) => {
          proxy.$modal.msgSuccess('新增成功')
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 取消按钮 */
function cancel() {
  open.value = false
  reset()
}

/** 把每一行的索引放进row */
function tableRowClassName({ row, rowIndex }) {
  row.index = rowIndex + 1
}

/** 多选框选中数据 */
function handleSelectionChange(selection) {
  sns.value = selection
    .map((item) => +item.index)
    .sort(function (a, b) {
      return a - b
    })
  ids.value = selection.map((item) => item.id)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 详细按钮操作 */
function handleView(row) {
  openView.value = true
  form.value = row
}

// 初始化列表
getList()
</script>
